/**
 * Created by ruwang on 10/19/14.
 */
var SPLATTER_COMMON_AND_CONSTANTS = require('./ConstantAndCommon.js');

var getMainBanner  = function () {
    require('./Banners.js').printHeadBanner();
}
var getMainMenuOptions = function (optionSet) {
    var rl = require('readline').createInterface({
        input: process.stdin,
        output: process.stdout
    });
    var optionDisplay = '';

    for(var propertyName in optionSet) {
        optionDisplay += '[' + optionSet[propertyName].option+ '] ' + optionSet[propertyName].display +'\n';
    }
    optionDisplay +='\n' + SPLATTER_COMMON_AND_CONSTANTS.Constants.TEXT.SELECT_YOUR_OPTION;

    var callBack = function (answer) {
        var answered = false;
        var optionCallback;
        for (var propertyName in optionSet) {
            if (optionSet[propertyName].option == answer) {
                answered = true;
                optionCallback = optionSet[propertyName].callback;
            }
        }
        if(!answered) {
            console.log('\n\n'+SPLATTER_COMMON_AND_CONSTANTS.Constants.TEXT.ERRORS.INCORRECT_OPTION+'\n\n');
            rl.question(optionDisplay, callBack);
        } else {
            rl.close();
            rl = null;
            if(typeof optionCallback == 'function'){
                optionCallback();
            }
        }
    };
    rl.question(optionDisplay, callBack);
}

module.exports.MainScreen = { Banner:false, MainMenuOptions:null, fullOperation: null };
module.exports.MainScreen.Banner = getMainBanner;
module.exports.MainScreen.MainMenuOptions = getMainMenuOptions;
module.exports.MainScreen.fullOperation = function() {
    getMainBanner();
    getMainMenuOptions(SPLATTER_COMMON_AND_CONSTANTS.Common.optionsAndAnswers.mainPage.optionSet);
}